﻿#include "study02.h"

// 条件语句
void test_conditional_statement() {
	print_start("条件语句");
	using namespace std;

	int x1 = 10;
	bool flag_x1 = x1 == 10; // flag_x1 表示 x1 等于 10 则为真，否则为假
	std::cout << "sizeof(bool) = " << sizeof(bool) << std::endl; // 1 字节
	std::cout << "flag_x1 = " << flag_x1 << std::endl;

	const char* ptr = nullptr;   // nullptr 表示空指针，同 NULL
	if (ptr == nullptr) // 空指针判断
		std::cout << "ptr is null" << std::endl;
	else
		std::cout << "ptr is not null" << std::endl;

	int a = 10, b = 20, c = 5;
	/*cout << "请输入三个整数：" << endl;
	cin >> a >> b >> c;  // 等待用户输入三个整数 */
	if (a > b && a > c)
		cout << "最大的数是：" << a << endl;
	else if (b > a && b > c)
		cout << "最大的数是：" << b << endl;
	else
		cout << "最大的数是：" << c << endl;
	print_end("条件语句");
}

// 循环语句
void test_loop_statement() {
	print_start("循环语句");
	for (int i = 0; i < 10; i++) // 循环 10 次
		std::cout << i << " ";
	std::cout << std::endl;

	int j = 10;
	while (j) // 无限循环
		std::cout << "j = " << j-- << " *** "; // --趋于退出
	std::cout << std::endl;

	int k = 0;
	do {
		std::cout << "k = " << k << "   ";
		k++;
	} while (k < 10);
	std::cout << std::endl;
	print_end("循环语句");
}

// 控制流语句
void test_jump_statement() { // 控制流语句（control break return goto）：
	print_start("控制流语句");
	int x = 0;
	while (true) // C++ 可以直接写 true 作为条件语句，这里无限循环
	{
		std::cout << "Hello, world!" << std::endl;
		++x;
		if (x >= 5) // 循环 5 次后退出
			break; // 跳出循环
	}

	for (int i = 0; i < 10; i++) // 循环 10 次
		if (i % 2 == 0) // 判断 i 是否为偶数
			continue; // 跳过偶数次循环，继续下一次循环
		else
			std::cout << "i = " << i << "   ";

	int day = 10;
	//again: // 标签语句
	//	std::cout << "请输入星期几（1-7）：" << std::endl;
	//	std::cin >> day;
	switch (day) // 选择语句
	{
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
		std::cout << "工作日" << std::endl;
		break;
	case 6:
	case 7:
		std::cout << "休息日" << std::endl;
		break;
	default:
		std::cout << "输入错误" << std::endl;
		//goto again; // 无条件跳转到 again 标签处
		break;
	}
	print_end("控制流语句");
}

// 函数局部变量
static CountStruct& get_singleton_count() {
	static CountStruct count;
	return count;
}

void test_local_variable() {
	print_start("函数局部变量");
	CountStruct& counter = get_singleton_count();
	std::cout << "Init counter = " << counter() << std::endl;
	std::cout << "++counter\n"; ++counter;
	counter.print();
	std::cout << "counter++\n"; counter++;
	counter.print();
	std::cout << "counter--\n"; counter--;
	counter.print();

	// 测试单例
	CountStruct counter2 = get_singleton_count();
	std::cout << "Init counter2 = " << counter2.value() << std::endl;
	std::cout << "counter2 == counter is " << ((counter2 == counter) == false ? "false" : "true") << std::endl;
	CountStruct* counter_ptr1 = &get_singleton_count();
	CountStruct* counter_ptr2 = &get_singleton_count();
	std::cout << "*counter_ptr1 == *counter_ptr2 is " << ((*counter_ptr1 == *counter_ptr2) ? "true" : "false") << std::endl;

	// 测试后置递增
	std::cout << "counter3 = counter2++\n";
	CountStruct counter3 = counter2++;
	std::cout << "counter2 = " << counter2.value() << std::endl;
	std::cout << "counter3 = " << counter3.value() << std::endl;

	// 链式调用
	std::cout << "((counter += 5) -= 2).value() = " << ((counter += 5) -= 2).value() << std::endl;
	//delete counter;
	print_end("函数局部变量");
}
